package com.buaa.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.buaa.multitythread.ImageDownload;
import com.buaa.multitythread.R;
import com.buaa.util.NetUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/20.
 */
public class MyAdapter extends BaseAdapter{
    private Context ctx;
    private LayoutInflater layoutInflater;
    private List<Map<String,Object>> list;

    public void setData(List<Map<String, Object>> list) {
        this.list = list;
    }

    public MyAdapter(Context ctx) {
        this.ctx = ctx;
        layoutInflater=layoutInflater.from(ctx);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView=layoutInflater.inflate(R.layout.item,null);
        }
        TextView name=(TextView)convertView.findViewById(R.id.name);
        TextView price=(TextView)convertView.findViewById(R.id.price);
        TextView address=(TextView)convertView.findViewById(R.id.address);
        final ImageView image=(ImageView)convertView.findViewById(R.id.image);

        name.setText(list.get(position).get("proname").toString());
        price.setText(list.get(position).get("proprice").toString());
        address.setText(list.get(position).get("proaddress").toString());

        ImageDownload imageDownload=new ImageDownload(NetUtil.IMAGEURL+list.get(position).get("proimage").toString());
        imageDownload.loadImage(new ImageDownload.ImageCallBack() {
            @Override
            public void getDrawable(Drawable drawable) {
               image.setImageDrawable(drawable);
            }
        });
        return convertView;
    }
}
